with IO; use IO;
with Ada.Streams.Stream_IO;
with Interfaces; use Interfaces;
with Block; use Block;
package PES is

   -- ref : http://www.mpucoder.com/DVD/pes-hdr.html
   -- http://happy.emu.id.au/lab/tut/dttb/dtbtut4b.htm#s4p4

   type PES_Header_Type is
      record
         Start_Code : Byte_Array(0..2) := (0,0,1);  -- 3
         Stream_Id : Byte_Type := 2#11000000#;      -- 1
         Length : Word_16 := 0;           -- 2
      end record;
   --pragma Pack(PES_Header_Type);
   --for PES_Header_Type'Size use 6**8;

   --subtype PES_Header_PTS_DTS_Type is Unsigned_32;

   type PES_Header_Extension_Type is
      record
         Start_Code : Bit_Array(1..2) := (1,0);
         Scramblin_Contr : Bit_Array(1..2) := (0,0);
         Priority : Bit_Type := 0;
         Data_Alignment : Bit_Type := 0;
         Copyright : Bit_Type := 0;
         Orig_Copy : Bit_Type := 0;
                                                   -- 1
         PTS_DTS : Bit_Array(1..2) := (0,0);
         ESCR : Bit_Type := 0;
         ES_Rate : Bit_Type := 0;
         DSM_Mode : Bit_Type := 0;
         Copy_Info : Bit_Type := 0;
         PES_CRC : Bit_Type := 0;
         PES_Extension : Bit_Type := 0;
                                                    -- 1
         -- stuffing bytes to follow 0xFF
         PES_Header_Data_Length : Byte_Type := 0;   -- 1

         -- optional extension. Depends on PTS/DTS
         PTS_Field : Unsigned_32 := 0;
         DTS_Field : Unsigned_32 := 0;
      end record;
   procedure Write_PES_Header_Extension_Type(Stream : not null access Ada.Streams.Root_Stream_Type'Class; Item : in PES_Header_Extension_Type);
   for PES_Header_Extension_Type'Write use Write_PES_Header_Extension_Type;
   --pragma Pack(PES_Header_Extension_Type);

   type PES_Type is
      record
         Header : PES_Header_Type;
         Extension : PES_Header_Extension_Type;
         Start_Pos : Ada.Streams.Stream_IO.Positive_Count := 1;
         Size : Natural := 0;
      end record;

--   type PES_Array is array (Positive range <>) of PES_Type;


--     type PES_Buffer_Type(Buffer_Size : Natural) is
--        record
--           PES_Units : PES_Array(1..Buffer_Size); -- could be made smaller
--           Size : Natural := 0;
--           Access_Unit : Byte_Buffer_Type(Buffer_Size);
--        end record;
--
--     procedure PES_Generator(Buffer : in out PES_Buffer_Type; PTS_Counter : in Unsigned_32);

   function Create(Block : in Block_Type; Time_Code : in Unsigned_32) return PES_Type;

private
   PES_SIZE : constant Positive := (2**16) - 1; -- max bytes
   PES_HEADER_SIZE : constant Positive := 6; -- header bytes
   PES_HEADER_EXT : constant Positive := 3; -- extension bytes
   PES_HEADER_PTS : constant Positive := 5; -- PTS bytes
   PES_HEADER_DTS : constant Positive := 5; -- DTS bytes
   Stream_ID : constant Byte_Type := 16#06#;   -- private stream 16#bd# (1011 1101)
   -- ATSC descriptor tag stream_type - PMT types 1000 0001 (0x81) DTS-0x85
   -- DVB-T 0x06 + descriptor

   procedure Write_PES_Header_PTS_DTS_Type(Stream : not null access Ada.Streams.Root_Stream_Type'Class; Item : in PES_Header_Extension_Type);
end PES;